//
//  FindViewModel.m
//  zhuchiren
//
//  Created by rainday on 15/1/30.
//  Copyright (c) 2015年 rainday. All rights reserved.
//

#import "FindViewModel.h"
#import <AFNetworking.h>
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>
#import "IP.h"
@interface FindViewModel(){

    NSString *_url;
}

@end
@implementation FindViewModel


- (id)init{
    self =[super init];
    if (self) {
        _hasLoaded = NO;
        _url = [NSString stringWithFormat:@"%s/user",SERVER];
        _dataArray = [NSMutableArray array];
        _isEmpty = NO;
        
    }
    return self;
}

- (void)fetchDataWithUserName:(NSString *)username{
    
    
   
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    id token = [userDefaults objectForKey:@"token"];
    
    
    NSDictionary *params = @{@"nickname":username};
    
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    
    [manager.requestSerializer setValue:[NSString stringWithFormat:@"Token %@",token] forHTTPHeaderField:@"Authorization"];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    
    __weak FindViewModel *weakSelf = self;
    
    [manager GET:_url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [weakSelf performSelectorOnMainThread:@selector(resetData:) withObject:[responseObject objectForKey:@"user_list"] waitUntilDone:YES];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];

}

- (void)resetData:(id)array{
    [_dataArray removeAllObjects];
    if ([array isKindOfClass:[NSArray class]]) {
        
        
        if ([array count]==0) {
            self.isEmpty = YES;
            return;
            
            
        }
        for (id item in array) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                NSDictionary *dic = [NSDictionary dictionaryWithDictionary:item];
                [_dataArray addObject:dic];
            }
            
            
        }
        self.hasLoaded = YES;
    }
}


- (NSDictionary *)dictionaryAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger row = indexPath.row;
    if ([_dataArray count]>row) {
        return (NSDictionary *)(_dataArray[row]);
        
    }
    return nil;
}

- (NSInteger)numberOfTable{
    return [_dataArray count];
    
}


+ (RACSignal *)collectUser:(NSString *)username{
  return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
      NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
      id token = [userDefaults objectForKey:@"token"];
      
      
      NSDictionary *params = @{@"username":username};
      
      
      
      AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
      manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
      manager.requestSerializer = [AFHTTPRequestSerializer serializer];
      
      [manager.requestSerializer setValue:[NSString stringWithFormat:@"Token %@",token] forHTTPHeaderField:@"Authorization"];
      [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
      [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
      
      [manager POST:[NSString stringWithFormat:@"%s/focus",SERVER] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
          [subscriber sendNext:responseObject];
          
          
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          
          [subscriber sendError:error];
      }];

      
      return nil;
  }];
}


- (void)fetchCollection{
    
    
    
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    id token = [userDefaults objectForKey:@"token"];
    
    
    
    
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    
    [manager.requestSerializer setValue:[NSString stringWithFormat:@"Token %@",token] forHTTPHeaderField:@"Authorization"];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    
    __weak FindViewModel *weakSelf = self;
    
    [manager GET:[_url stringByReplacingOccurrencesOfString:@"user" withString:@"focus"] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [weakSelf performSelectorOnMainThread:@selector(resetData:) withObject:[responseObject objectForKey:@"list"] waitUntilDone:YES];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
    
}


@end
